"""
URL configuration for xingong project.

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/5.2/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
from rest_framework.routers import DefaultRouter
from rest_framework import permissions
from drf_yasg.views import get_schema_view
from drf_yasg import openapi

from users import api_views as user_api
from announcement import api_views as announcement_api
from certificate import api_views as certificate_api
from extra_review import api_views as extra_review_api
from grade_detail import api_views as grade_api
from main import api_views as main_api
from review_record import api_views as review_record_api
from . import cos_views

# REST API路由器
router = DefaultRouter()
router.register(r'users', user_api.UserViewSet, basename='api-user')
router.register(r'announcements', announcement_api.AnnouncementViewSet, basename='api-announcement')
router.register(r'certificates', certificate_api.CertificateViewSet, basename='api-certificate')
router.register(r'extra-reviews', extra_review_api.ExtraReviewViewSet, basename='api-extra-review')
router.register(r'grades', grade_api.GradeDetailViewSet, basename='api-grade')
router.register(r'colleges', main_api.CollegeViewSet, basename='api-college')
router.register(r'majors', main_api.MajorViewSet, basename='api-major')
router.register(r'classes', main_api.ClassesViewSet, basename='api-class')
router.register(r'courses', main_api.CourseViewSet, basename='api-course')
router.register(r'punishments', main_api.PunishViewSet, basename='api-punish')
router.register(r'punish-dicts', main_api.PunishDictViewSet, basename='api-punish-dict')
router.register(r'scores', main_api.ScoreViewSet, basename='api-score')
router.register(r'review-records', review_record_api.ReviewRecordViewSet, basename='api-review-record')

# Swagger API文档配置
schema_view = get_schema_view(
    openapi.Info(
        title="综测系统 API 文档",
        default_version='v1',
        description="""学生综合测评管理系统后端API接口文档
        
## 认证方式
使用Token认证，在请求头中添加：`Authorization: Token <your-token>`

## 主要功能模块
- **用户管理**: 用户CRUD、权限管理
- **公告管理**: 公告发布、查看、编辑
- **证书管理**: 证书字典维护
- **附加分审查**: 三级审核流程（班委→辅导员→学工办）
- **成绩管理**: 成绩录入、统计
- **综测分管理**: 综测分计算、排名
- **基础数据**: 学院、专业、班级、课程、处分管理
        """,
        terms_of_service="https://www.example.com/policies/terms/",
        contact=openapi.Contact(email="admin@example.com"),
        license=openapi.License(name="MIT License"),
    ),
    public=True,
    permission_classes=(permissions.AllowAny,),
)

urlpatterns = [
    # Django Admin
    path('admin/', admin.site.urls),
    
    # Swagger API文档
    path('swagger/', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
    path('redoc/', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'),
    path('swagger.json', schema_view.without_ui(cache_timeout=0), name='schema-json'),
    
    # API认证接口
    path('api/auth/login/', user_api.login, name='api-login'),
    path('api/auth/logout/', user_api.logout, name='api-logout'),
    path('api/auth/me/', user_api.current_user, name='api-current-user'),
    
    # COS上传接口
    path('api/cos/upload/', cos_views.upload_to_cos, name='api-cos-upload'),
    path('api/cos/proxy-download/', cos_views.proxy_download, name='api-cos-proxy-download'),
    
    # API路由
    path('api/', include(router.urls)),
    
    # 传统页面路由
    path('users/', include('users.urls')),
]

# 媒体文件访问
if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
